import Mock from "mockjs"

const list = []
const count = 100
/**
 * 拓展mock
 */
Mock.Random.extend({
  phone: function() {
    const phonePrefixs = ["139", "155", "172"] // 手机号前缀
    return this.pick(phonePrefixs) + Mock.mock(/\d{8}/) //Number()
  }
})

for (let i = 0; i < count; i++) {
  list.push(
    Mock.mock({
      userId: "@increment",
      nickname: "@cname",
      phone: "@phone",
      email: "@email",
      permissions: ["*"],
      roles: ["*"],
      updateTime: "@datetime"
    })
  )
}

export default [
  // user login
  {
    url: "/login",
    type: "post",
    response: config => {
      // const {username} = config.body

      return {
        code: "SUCCESS",
        message: "成功",
        data: [{
          tokenValue: "1234567"
        }]
      }
    }
  },

  // get user info
  {
    url: "/current",
    type: "get",
    response: config => {
      // const {token} = config.query

      return {
        code: "SUCCESS",
        message: "成功",
        data: [
          {
            userId: 1,
            username: "admin",
            nickname: "超级管理员",
            phone: "17269595916",
            email: "haojundd@qq.com",
            permissions: ["*"],
            roles: ["*"],
            avatar: "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
          }
        ]
      }
    }
  },

  // user logout
  {
    url: "/logout",
    type: "get",
    response: () => {
      return {
        code: "SUCCESS",
        message: "成功"
      }
    }
  },

  // user page
  {
    url: "/user/page",
    type: "post",
    response: config => {
      const { pageIndex = 1, pageSize = 10 } = config.body

      const pageList = list.filter((item, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1))

      return {
        code: "SUCCESS",
        message: "成功",
        total: count,
        data: pageList
      }
    }
  }
]
